package com.ly.emp.controller.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.common.core.utils.excel.ExcelResult;
import com.ly.common.core.utils.excel.ExcelUtil;
import com.ly.emp.entity.EpmBasePeriod;
import com.ly.emp.service.IEpmBasePeriodService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考核周期
 *
 * @author luoyun
 * @Date: 2025-05-13
 */
@Tag(name = "考核周期")
@Slf4j
@RestController
@RequestMapping("/emp/epmBasePeriod")
@RequiredArgsConstructor
public class EpmBasePeriodController {

    private final IEpmBasePeriodService epmBasePeriodService;

    /**
     * 查询-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询-id")
    @GetMapping("/getId")
    public R<EpmBasePeriod> getId(@RequestParam(name = "id") String id) {
        return epmBasePeriodService.getId(id);
    }

    /**
     * 分页
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("/getPage")
    public R<IPage<EpmBasePeriod>> getPage(
            EpmBasePeriod entity,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return epmBasePeriodService.getPage(entity, new Page<EpmBasePeriod>(pageNo, pageSize));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    public R<String> add(@RequestBody EpmBasePeriod entity) {
        return epmBasePeriodService.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping("/edit")
    public R<String> edit(@RequestBody EpmBasePeriod entity) {
        return epmBasePeriodService.edit(entity);
    }

    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除-id")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return epmBasePeriodService.delete(id);
    }

    /**
     * 批量新增或修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "批量新增或修改")
    @PostMapping("/updateBatch")
    public R updateBatch(@Validated @RequestBody CommonBatchUpdateDTO<EpmBasePeriod> dto) {
        return epmBasePeriodService.updateBatch(dto);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        return epmBasePeriodService.deleteBatch(ids);
    }

    /**
     * 导出excel
     *
     * @param response
     */
    @Operation(summary = "导出excel")
    @GetMapping("/exportXls")
    public void exportXls(HttpServletResponse response) {
        List<EpmBasePeriod> list = epmBasePeriodService.list();
        ExcelUtil.exportExcel(list, "考核周期表", EpmBasePeriod.class, response);
    }

    /**
     * 导入excel
     *
     * @param file
     * @param updateSupport：是否删除标识
     * @return
     */
    @Operation(summary = "导入excel")
    @PostMapping("/importExcel")
    public R<?> importWork(MultipartFile file, boolean updateSupport) throws Exception {
        // 监听： new ImportListener(updateSupport);
        ExcelResult<EpmBasePeriod> result = ExcelUtil.importExcel(file.getInputStream(), EpmBasePeriod.class, null);
        return R.ok(true, result.getAnalysis());
    }

    @Operation(summary = "获取考核周期树：year年；yearM年+月；yearQ年季度")
    @PostMapping("/periodTree")
    public R periodTree(@RequestParam(name = "type") String type) {
        return R.ok(epmBasePeriodService.periodTree(type));
    }
}
